<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="header">
			<view class="back-icon" @click="goBack">
				<text class="iconfont">←</text>
				<text class="back-text">返回</text>
			</view>
			<view class="title">订单详情</view>
			<view class="website">read.yangtse.com</view>
		</view>
		
		<!-- 订单详情内容 -->
		<view class="order-detail" v-if="orderInfo">
			<view class="detail-card">
				<view class="card-header">
					<view class="order-id">订单号：{{orderInfo.id}}</view>
					<view class="order-status" :class="[getStatusClass(orderInfo.status)]">{{getStatusText(orderInfo.status)}}</view>
				</view>
				
				<view class="detail-section">
					<view class="section-title">基本信息</view>
					<view class="info-list">
						<view class="info-item">
							<text class="info-label">分类：</text>
							<text class="info-value">{{orderInfo.category}} - {{orderInfo.subCategory}}</text>
						</view>
						<view class="info-item">
							<text class="info-label">刊登日期：</text>
							<text class="info-value">{{orderInfo.publishDate}}</text>
						</view>
						<view class="info-item">
							<text class="info-label">刊登天数：</text>
							<text class="info-value">{{orderInfo.publishDays}}天</text>
						</view>
						<view class="info-item">
							<text class="info-label">创建时间：</text>
							<text class="info-value">{{formatDate(orderInfo.createTime)}}</text>
						</view>
						<view class="info-item">
							<text class="info-label">价格：</text>
							<text class="info-value price">¥{{orderInfo.price}}</text>
						</view>
					</view>
				</view>
				
				<view class="detail-section">
					<view class="section-title">内容信息</view>
					<view class="content-box">
						<text class="content-text">{{orderInfo.content || '暂无内容'}}</text>
					</view>
				</view>
				
				<view class="detail-section">
					<view class="section-title">联系信息</view>
					<view class="info-list">
						<view class="info-item">
							<text class="info-label">联系人：</text>
							<text class="info-value">{{orderInfo.contactName || '未提供'}}</text>
						</view>
						<view class="info-item">
							<text class="info-label">联系电话：</text>
							<text class="info-value">{{orderInfo.contactPhone || '未提供'}}</text>
						</view>
					</view>
				</view>
			</view>
			
			<!-- 操作按钮 -->
			<view class="action-buttons">
				<view class="action-btn contact-btn" @click="contactCustomerService">
					<text>联系客服</text>
				</view>
				<view class="action-btn back-btn" @click="goBack">
					<text>返回列表</text>
				</view>
			</view>
		</view>
		
		<!-- 加载中提示 -->
		<view class="loading-container" v-else-if="isLoading">
			<view class="loading-text">加载中...</view>
		</view>
		
		<!-- 无数据提示 -->
		<view class="error-container" v-else>
			<image src="/static/error.png" mode="aspectFit" class="error-image"></image>
			<text class="error-text">获取订单详情失败</text>
			<view class="go-back-btn" @click="goBack">
				<text>返回列表</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				orderInfo: null,
				isLoading: true,
				statusMap: {
					0: '待支付',
					1: '已支付',
					2: '已取消',
					3: '已完成',
					4: '处理中'
				}
			}
		},
		onLoad(options) {
			if (options.id) {
				this.id = options.id;
				this.loadOrderDetail();
			} else {
				this.isLoading = false;
				uni.showToast({
					title: '参数错误',
					icon: 'none'
				});
			}
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			loadOrderDetail() {
				this.isLoading = true;
				this.orderInfo = null; // 清空旧数据
				
				// 调用云函数获取订单详情
				uniCloud.callFunction({
					name: 'getOrderDetail', // 确保您已创建此云函数
					data: { id: this.id }
				}).then(res => {
					console.log('获取订单详情成功：', res);
					uni.hideLoading(); // 如果之前有 showLoading
					this.isLoading = false;
					
					if (res.result && res.result.code === 0 && res.result.data) {
						this.orderInfo = res.result.data;
					} else {
						console.error('获取订单详情数据失败或格式错误：', res.result);
						uni.showToast({
							title: res.result ? res.result.message : '加载订单详情失败',
							icon: 'none'
						});
					}
				}).catch(err => {
					console.error('调用云函数getOrderDetail失败：', err);
					uni.hideLoading(); // 如果之前有 showLoading
					this.isLoading = false;
					uni.showToast({
						title: '加载订单详情出错',
						icon: 'none'
					});
				});
			},
			formatDate(date) {
				if (!date) return '';
				if (typeof date === 'string') {
					date = new Date(date);
				}
				return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
			},
			getStatusClass(status) {
				const classMap = {
					0: 'status-pending',
					1: 'status-paid',
					2: 'status-cancelled',
					3: 'status-completed',
					4: 'status-processing'
				};
				return classMap[status] || '';
			},
			getStatusText(status) {
				return this.statusMap[status] || '未知状态'
			},
			contactCustomerService() {
				uni.makePhoneCall({
					phoneNumber: '13776769686'
				});
			}
		}
	}
</script>

<style lang="scss">
	.container {
		min-height: 100vh;
		background-color: #f8f8f8;
		padding-bottom: 40rpx;
	}
	
	.header {
		background: linear-gradient(135deg, #2ecc71, #27ae60);
		color: #fff;
		padding: 40rpx 30rpx;
		display: flex;
		align-items: center;
		position: relative;
		box-shadow: 0 2px 10px rgba(0,0,0,0.1);
		
		.back-icon {
			display: flex;
			align-items: center;
			background-color: rgba(255, 255, 255, 0.2);
			padding: 10rpx 20rpx;
			border-radius: 30rpx;
			margin-right: 10rpx;
			
			.iconfont {
				font-size: 36rpx;
				font-weight: bold;
			}
			
			.back-text {
				font-size: 28rpx;
				margin-left: 10rpx;
			}
			
			&:active {
				background-color: rgba(255, 255, 255, 0.3);
			}
		}
		
		.title {
			flex: 1;
			text-align: center;
			font-size: 34rpx;
			font-weight: 500;
		}
		
		.website {
			position: absolute;
			bottom: 10rpx;
			right: 30rpx;
			font-size: 24rpx;
			opacity: 0.8;
		}
	}
	
	.order-detail {
		padding: 30rpx;
	}
	
	.detail-card {
		background: #fff;
		border-radius: 12rpx;
		overflow: hidden;
		box-shadow: 0 2px 8px rgba(0,0,0,0.05);
		margin-bottom: 30rpx;
		
		.card-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 30rpx;
			border-bottom: 1px solid #f5f5f5;
			
			.order-id {
				font-size: 28rpx;
				color: #666;
			}
			
			.order-status {
				font-size: 26rpx;
				font-weight: 500;
				
				&.status-pending {
					color: #FFA500;
				}
				
				&.status-paid {
					color: #4CAF50;
				}
				
				&.status-processing {
					color: #9b59b6;
				}
				
				&.status-completed {
					color: #2196F3;
				}
				
				&.status-cancelled {
					color: #999999;
				}
			}
		}
	}
	
	.detail-section {
		padding: 20rpx 30rpx 30rpx;
		border-bottom: 1px solid #f5f5f5;
		
		&:last-child {
			border-bottom: none;
		}
		
		.section-title {
			font-size: 30rpx;
			color: #333;
			font-weight: 500;
			margin-bottom: 20rpx;
		}
		
		.info-list {
			.info-item {
				display: flex;
				margin-bottom: 15rpx;
				
				&:last-child {
					margin-bottom: 0;
				}
				
				.info-label {
					font-size: 26rpx;
					color: #999;
					width: 160rpx;
				}
				
				.info-value {
					font-size: 26rpx;
					color: #333;
					flex: 1;
					
					&.price {
						color: #e74c3c;
						font-weight: 500;
						font-size: 28rpx;
					}
				}
			}
		}
		
		.content-box {
			background: #f9f9f9;
			padding: 20rpx;
			border-radius: 8rpx;
			
			.content-text {
				font-size: 26rpx;
				color: #333;
				line-height: 1.6;
			}
		}
	}
	
	.action-buttons {
		display: flex;
		justify-content: space-between;
		
		.action-btn {
			flex: 1;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 8rpx;
			font-size: 28rpx;
			font-weight: 500;
			margin: 0 15rpx;
			
			&.contact-btn {
				background: #2ecc71;
				color: #fff;
				
				&:active {
					opacity: 0.9;
				}
			}
			
			&.back-btn {
				background: #f5f5f5;
				color: #666;
				
				&:active {
					background: #f0f0f0;
				}
			}
		}
	}
	
	.loading-container, .error-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 0;
		
		.loading-text, .error-text {
			font-size: 28rpx;
			color: #999;
			margin: 30rpx 0;
		}
		
		.error-image {
			width: 200rpx;
			height: 200rpx;
		}
		
		.go-back-btn {
			padding: 20rpx 60rpx;
			background: #2ecc71;
			color: #fff;
			border-radius: 8rpx;
			font-size: 28rpx;
			margin-top: 30rpx;
			
			&:active {
				opacity: 0.9;
			}
		}
	}
</style> 